﻿using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO; 
using MyLib;

namespace CapexProcess.Dialogs
{
    /// <summary>
    /// GenerateReports 的摘要说明
    /// </summary>
    public class GetAttachmentName : IHttpHandler 
    {
        string strSql;
        string strJson;
        string strReason;
        string strCurrProcessName;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            strReason = context.Request["Reason"];            //Capex单上用户选择的原因
            strCurrProcessName = context.Request["ProcessName"];           //当前流程名
            if (strReason != "")
            {
                strSql = "select ATTACHMENTENGLISHNAME from SYNC_ATTACHMENT_DT "
                           + "  where PROCESSNAME='" + strCurrProcessName.Trim() + "' and REASON='" + strReason.Trim() + "' and ISACTIVE=1 ";
            }
            else
            {
                strSql = "select ATTACHMENTENGLISHNAME from SYNC_ATTACHMENT_DT "
                           + "  where PROCESSNAME='" + strCurrProcessName.Trim() + "' and ISACTIVE=1 ";
            }           
            DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable(strSql.ToString());
            string strAttachmentEnglishName = "";
            if (dt.Rows.Count>0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    strAttachmentEnglishName += dt.Rows[i]["ATTACHMENTENGLISHNAME"].ToString().Trim() + ",";                    
                }
            }
            strAttachmentEnglishName = strAttachmentEnglishName.TrimEnd(',');


            //如果存在返回值的情况
            if (dt.Rows.Count > 0)
            {             
                 
                strJson = "[";
                strJson += "{'AttEngListName':'" + CheckText(strAttachmentEnglishName.Trim()) + "'}";
                
                //strJson += "{'ProcessName':'" + CheckText(strProcessName.Trim()) + "',"
                 //       + "'AvgDay':'" + CheckText(strAvgDay.Trim()) + "'},";            
               // strJson = strJson.Substring(0, strJson.LastIndexOf(','));
                strJson += "]";
            }

            context.Response.Buffer = true;
            context.Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
            context.Response.Expires = 0;
            context.Response.CacheControl = "no-cache";
            context.Response.Write(strJson);
        }       

        public string CheckText(string Str)
        {
            string ReturnString = "";
            ReturnString = Str.Replace(",", "#dou#");
            ReturnString = Str.Replace("'", "#dan#");
            return ReturnString;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
         
    }
}